Today I'm going to make an NPC that follows around other characters.
So if you get within a distance of interest, which is 30 studs, he's going to start following you
or any character that's within that distance.
He'll go after the closest one.
So if I get close.
Oh man, now I got a friend.
I can't get rid of him either.
Death will get rid of them.
Right.
But I'm going to go see if I can get Ralph in between me and him.
And then he'll be more interested in Ralph.
There we go.
That's pretty cool.
I and Ralph was moving slow, so he has to keep stopping.
I thought that would be pretty good.
Let's go ahead and get started.
All right.
So I got my MPC work world up.
I haven't I haven't imported any of these into my game yet.
I figured I'd do a few of them.
So we have Ralph right here, and he does this simple patrol.
I'm going to use Ralph.
And if you didn't get him working from the last video, I'll put a link in a file, the URLs file for
Ralph where you can get them in your toolbox, and then just just get them from there.
So we'll go ahead and do that.
Like if you go to the link in the description, you'll go to roadblocks and then you hit the green jet
button and then you go ahead and you'll have Ralph in your inventory.
Yep.
And there's two scripts.
We made them in one of the videos.
All right, Now, this is not going to be Ralph.
I'm going to rename them.
I'm going to name them.
I don't know Cletus.
I don't even know if that's I spell Cletus.
We know that.
That's that's what it is, though.
I'm going to get rid of my thumbnail camera because I don't have it set and then I'm going to get rid
of the waypoints because we're not going go around them.
And then I change the body colors.
I think I used what, yellow and the demo I did, they're just so that they're different.
We don't have the names up.
So this is Cletus and he's looking for a friend.
We're going to go to that move NPC where we did the patrol.
It'll air now, right, Because we don't have waypoints.
That's all right.
I'm going to get rid of just about everything in here.
We just wanted that animate script set.
We want to have our humanoid root part unanchored, things like that so you can move around.
All right.
So I'm going to have a character, the character that he's chasing.
So I've got to be more specific.
I'm going to call this I NPC.
There's going to be a little bit of eye, right?
He's going to he's going to make the decision to find somebody and then follow them.
So I'm also going to get the humanoid root part.
I'm going to call that I hrp and we'll say I NPC dot, humanoid root part and then I'm going to get
the eye him from the I NPC.
There we go.
I humanoid and I'm not doing the weight for child because it's on server side.
We're not doing spawning and stuff like that.
But just be aware that if you start getting errors that the humanoid root part or the humanoid is not
not available, you'll have to start doing those checks right with wait for child or find first child.
Let's get a max distance where he pays attention to people.
So I'll do a local max dist equals and I'll get it.
30 studs.
Now I'm going to need a function to find the nearest NPC or character.
I'll call it find nearest character.
Let's go local function, find nearest char and this is going to be playing characters and non playing
characters.
So I'm going to pass in my i NPC in case I want to move this to a module script.
I want it to be self contained.
So all the arguments in that I'm going to use within the function I'm going to pass in, I'm also going
to pass in the distance of interest.
I'll call it dist of int.
Now I'm going to need a variable called closest.
Let's call it close this HRP closest humanoid root part will key off of that because that holds the
information for position and orientation of characters.
Now I get a I and C variable, right?
And it will get our humanoid root part and we'll get that from the I NPC dot humanoid root part.
We have it up here, but like I said, I might want to move this into a module script so that all NPCs
could find nearest character or all monsters.
May we may want them to find the character using this function.
No need to write it 50 times for 50 different characters.
Just move it into a module script.
So let's do a for loop and I'm going to say for I and V in pairs.
I'm going to look inside the workspace.
I'm going to get the children in the workspace.
So basically I'm getting all the children in the workspace.
This is not the most efficient way of looking through the workspace.
We'll optimize this later, so I'll show you how to optimize it if we don't have time for that.
So I want to get a humanoid root part.
I want to check to see if any of the things in the workspace has a humanoid root part.
As a first child, that means it's a character.
So I'm going to say Fine.
First child of V, right?
V is the thing.
We're going to check to see if there is a humanoid root part associated with the thing.
If there is, it's something worth following.
So I'm going to say if HR P fan, let's see what the distance is to that HRP from r I NPCs humanoid
root part.
So I'm going to say local temp d distance equals i npc humanoid root part dot position minus the one
we just found, the HPP we just found in V's possession.
And then I'm going to do magnitude that gets the distance between the two points.
Then I check if the distance is less than the distance of interest and temp distance is greater than
one, then why did I do that?
Well, this one makes sense, right?
We want to make sure the temp distance is within our distance of interest, but we also want to make
sure it's not us, right, So we can find our self.
So we have to make sure we're at least one stud away.
And you could make it two if you wanted to.
That would be fine.
But we want to make sure it's at least one start away.
Then what we're going to do if they are if that does meet the criteria, they're within our distance,
but not us.
We are going to make the distance of interest now, temp distance.
So it got a little closer.
So now the next loop around in our for loop, we're only going to look for things that are closer than
the last temp distance.
That's how we find the closest one.
All right, so then I'm going to get my closest hr P, and it's going to be that hr P.
What I what I'm going to do when I get down to the bottom here.
After that for loop, I'm going to return to closest air up.
Now, if we didn't find any, that's fine.
This is going to return nil.
Let's scroll down a little bit and make a while loop and I'll do while I'm going to put my weight statement
right in the while loop.
So the weight statement will act as true for my boolean because it is returning a value.
So it considers that as something.
So that's true.
But we're going to wait in between each loop iteration for one second.
So it's kind of handy because that way you don't forget to put your weight in and then crash your machine.
I'm going to do a local closest hr P equal to find nearest char.
You know what?
I should have called that fine nearest HPE.
That would have made more sense, but I'm going to stick with it now.
So I'm going to pass in my i NPC that's defined above and my max distance, which will become the distance
of interest when it gets inside the function.
And then if I get a closest hr p back where within distance of something we're going to get a target
position that we want to go to.
So I'll say target position is the closest HR position and then I want the direction to so let's say
local I d r, So that's my eyes direction where he wants to go and we get my target position minus the
I humanoid route part position.
Dot unit.
And the reason I do this unit is because I want this direction vector to be of length one so I can calculate
how many studs back I want them to stop.
I don't want them to run right into them, but if I don't know the length of this vector, I can't subtract
from my stop position.
That's why I'm doing the unit vector.
Now let's get our AI humanoid and do a move to to get our little AI guy running after the humanoid that
he found the HRP that he found.
So we have two arguments in this move to.
And one of them, if the target's not moving, we can get our target position and move to it.
But remember, we don't want to run right into them.
So let's subtract off I direction times five.
So remember I direction is one stud away.
If we multiply it by five or five studs away, that's cool and all.
But what happens if the guy is moving around and they probably will be?
Well, the second argument is a walk to part.
So this is walk to point.
And if we add a walk to part and the parts moving, it's going to update my walk to point.
It's pretty convenient.
So the closest humanoid route part will be the point that we're going to chase around.
All right.
Let's go ahead and try this out.
I'm going to go to my MPC work and luckily, I got a spawn location right in front of Cletus so I don't
have to run and find them.
Hey, we'll start our game.
There he is.
Here he goes.
And we're running around and he's chasing us.
That's pretty cool.
Oh, and I found out that the move to times out on 8 seconds, I think I said 6 seconds.
And in the other video, I just read that.
So there you go.
You got to you got somebody that's going to be able to chase you around.